package com.buss.customer.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;
import com.buss.customer.dao.CustomerClassDao;
import com.buss.customer.model.CustomerClass;

@Repository
public class CustomerClassDaoImpl implements CustomerClassDao {

	@Autowired
	private BaseDao<CustomerClass> baseDao;
	
	@Override
	public CustomerClass saveClass(CustomerClass customerClass) {
		return baseDao.saveOrEdit(customerClass);
	}

	@Override
	public void editClass(CustomerClass customerClass) {
		baseDao.edit(customerClass);
	}

	@Override
	public void removeClass(Long[] ids) {
		String sql = "delete from CustomerClass customerclass where customerclass.id in (:ids)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		baseDao.excuteHsql(sql, params);
	}

	@Override
	public CustomerClass loadClass(Long id) {		
		return baseDao.get(id, CustomerClass.class);
	}

	@Override
	public ResultList<CustomerClass> queryByParentId(String companyId,
			Long parentId, String name) {
		String sql = "select customerclass from CustomerClass customerclass where customerclass.companyId = :companyId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		if(parentId != null){
			sql += " and customerclass.parentId = :parentId ";
			params.put("parentId", parentId);
		}
		if(StringUtil.isNotEmpty(name)){
			sql += " and customerclass.name like :name ";
			params.put("name", "%" + name + "%");
		}
		return baseDao.queryHsql(sql, params, null);
	}

	@Override
	public void updateSubClasses(Long id, Integer num) {
		String sql = "update CustomerClass customerclass set customerclass.subClasses = customerclass.subClasses + :num where customerclass.id = :id";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("num", num);
		params.put("id", id);
		baseDao.excuteHsql(sql, params);
	}

}
